package cn.hhchat.wp.advice;

import cn.hhchat.wp.dto.msg.RMsg;
import cn.hhchat.wp.dto.msg.RMsgFactory;
import cn.hhchat.wp.dto.msg.ResultEnum;
import cn.hhchat.wp.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created this one by HMH on 2017/6/22.
 */
@Slf4j
@ControllerAdvice
public class ExceptionAdviceHandler {

    @ResponseBody
    @ExceptionHandler(Throwable.class)
    public RMsg handleUnexpectedServerError(Exception ex) {
        log.error(" 🌧 exception occurred: ", ex);
        if(ex instanceof BaseException) {
            return RMsgFactory.err(((BaseException) ex).getErrcode(),((BaseException) ex).getErrmsg());
        }
        return RMsgFactory.err(ResultEnum.UnKnowException,ex.getMessage());
    }

}
